﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class DAOKhachHang:DataProvider
    {
        public static DataTable DSKhachHang()
        {
            SqlConnection MoKetNoi = KetNoi();
            string chuoilenh = "select * from khachhang";
            DataTable listkh;
            SqlDataAdapter da = new SqlDataAdapter(chuoilenh, MoKetNoi);
            DataSet ds = new DataSet();
            da.Fill(ds);
            listkh = ds.Tables[0];
            DongKetNoi();
            return listkh;
        }
        public static int ThemKH(DTOKhachHang kh)
        {
            SqlConnection MoKetNoi = KetNoi();
            string chuoilenh = "insert into khachhang(hoten,diachi,DiemTichLuy) values(@hoten,@diachi,0)";
            SqlCommand lenh = new SqlCommand(chuoilenh, MoKetNoi);
            SqlParameter values = new SqlParameter("@hoten", kh.TenKH);
            lenh.Parameters.Add(values);
            values = new SqlParameter("@diachi", kh.DiaChi);
            lenh.Parameters.Add(values);
            int result = lenh.ExecuteNonQuery();
            DongKetNoi();
            return result;
        }
        public static int XoaKH(int id)
        {
            SqlConnection MoKetNoi = KetNoi();
            string chuoilenh = "delete from khachhang where MaKH=@id ";
            SqlCommand lenh = new SqlCommand(chuoilenh, MoKetNoi);
            SqlParameter values = new SqlParameter("@id", id);
            lenh.Parameters.Add(values);
            int result = lenh.ExecuteNonQuery();
            DongKetNoi();
            return result;
        }
        public static int SuaKH(DTOKhachHang kh)
        {
            SqlConnection MoKetNoi = KetNoi();
            string chuoilenh = "update khachhang set hoten=@ten, diachi=@address where MaKH=@id ";
            SqlCommand lenh = new SqlCommand(chuoilenh, MoKetNoi);
            SqlParameter values = new SqlParameter("@ten", kh.TenKH);
            lenh.Parameters.Add(values);
            values = new SqlParameter("@address", kh.DiaChi);
            lenh.Parameters.Add(values);
            values = new SqlParameter("@id", kh.MaKH);
            lenh.Parameters.Add(values);
            int result = lenh.ExecuteNonQuery();
            DongKetNoi();
            return result;
        }
    }
}
